function[I_1, I_2, I_3]=CropImage_4Cam_test(I1, I2, I3, R1, R2, R3,T,InM, flag)

[l,w,~]=size(I1);

p1_1=Rotate_Pts_Stereo([1,1],[l,w],R1,InM);
p1_2=Rotate_Pts_Stereo([1,l],[l,w],R1,InM);
p1_3=Rotate_Pts_Stereo([w,1],[l,w],R1,InM);
p1_4=Rotate_Pts_Stereo([w,l],[l,w],R1,InM);

if flag == 3
    p2_1=Rotate_Pts_Stereo([1,1],[l,w],R2,InM);
    p2_2=Rotate_Pts_Stereo([1,l],[l,w],R2,InM);
    p2_3=Rotate_Pts_Stereo([w,1],[l,w],R2,InM);
    p2_4=Rotate_Pts_Stereo([w,l],[l,w],R2,InM);
    
    p3_1=Rotate_Pts_Stereo_T([1,1],[l,w],R3, T, InM);
    p3_2=Rotate_Pts_Stereo_T([1,l],[l,w],R3, T, InM);
    p3_3=Rotate_Pts_Stereo_T([w,1],[l,w],R3, T, InM);
    p3_4=Rotate_Pts_Stereo_T([w,l],[l,w],R3, T, InM);
else
    p3_1=Rotate_Pts_Stereo([1,1],[l,w],R3,InM);
    p3_2=Rotate_Pts_Stereo([1,l],[l,w],R3,InM);
    p3_3=Rotate_Pts_Stereo([w,1],[l,w],R3,InM);
    p3_4=Rotate_Pts_Stereo([w,l],[l,w],R3,InM);
    
    p2_1=Rotate_Pts_Stereo_T([1,1],[l,w],R2, T, InM);
    p2_2=Rotate_Pts_Stereo_T([1,l],[l,w],R2, T, InM);
    p2_3=Rotate_Pts_Stereo_T([w,1],[l,w],R2, T, InM);
    p2_4=Rotate_Pts_Stereo_T([w,l],[l,w],R2, T, InM);
end

lp = ceil([max(max(max(p1_1(2),p1_3(2)), max(p2_1(2),p2_3(2))), max(p3_1(2),p3_3(2))), 
           max(max(max(p1_1(1),p1_2(1)), max(p2_1(1),p2_2(1))), max(p3_1(1),p3_2(1)))]);
rd = floor([min(min(min(p1_4(2),p1_2(2)), min(p2_4(2),p2_2(2))), min(p3_4(2),p3_2(2))),
            min(min(min(p1_3(1),p1_4(1)), min(p2_3(1),p2_4(1))), min(p3_3(1),p3_4(1)))]);

% lp = ceil([max(max(p1_1(2),p1_3(2)), max(p2_1(2),p2_3(2))), 
%            max(max(p1_1(1),p1_2(1)), max(p2_1(1),p2_2(1)))]);
% rd = floor([min(min(p1_4(2),p1_2(2)), min(p2_4(2),p2_2(2))),
%             min(min(p1_3(1),p1_4(1)), min(p2_3(1),p2_4(1)))]);

% lp = ceil([max(max(max(p1_1(2),p1_3(2)), max(p3_1(2),p3_3(2))), max(p2_1(2),p2_3(2))), 
%            max(max(max(p1_1(1),p1_2(1)), max(p3_1(1),p3_2(1))), max(p2_1(1),p2_2(1)))]);
% rd = floor([min(min(min(p1_4(2),p1_2(2)), min(p3_4(2),p3_2(2))), min(p2_4(2),p2_2(2))),
%             min(min(min(p1_3(1),p1_4(1)), min(p3_3(1),p3_4(1))), min(p2_3(1),p2_4(1)))]);

% lp = ceil([max(max(p1_1(2),p1_3(2)), max(p3_1(2),p3_3(2))), 
%            max(max(p1_1(1),p1_2(1)), max(p3_1(1),p3_2(1)))]);
% rd = floor([min(min(p1_4(2),p1_2(2)), min(p3_4(2),p3_2(2))),
%             min(min(p1_3(1),p1_4(1)), min(p3_3(1),p3_4(1)))]);

I_1=I1(lp(1):rd(1),lp(2):rd(2),:);
I_2=I2(lp(1):rd(1),lp(2):rd(2),:);
I_3=I3(lp(1):rd(1),lp(2):rd(2),:);

%%
windowrange_opencv = [lp(1),rd(1),lp(2),rd(2)];
matlab2opencv( windowrange_opencv, 'windowrange.yml');

end
